﻿using System;
using System.Collections.Generic;

namespace Borg.Framework
{
    public class UnplugCommand : ICommand
    {
        private readonly string[][] _arguments =
            new[]
                {
                    new[] { "ExtensionInfo", "Plug", "SlotInfo" },
                    new string[0],
                    new[] { "from" },
                    new[] { "ExtensionInfo", "Plug", "SlotInfo" }
                };

        public IEnumerable<string> GetArguments(string[] command, int pos)
        {
            string[] strArray = null;

            switch (pos)
            {
                case 0:
                case 2:
                case 3:
                    return _arguments[pos];

                case 1:
                case 4:
                    if (command.Length >= (pos + 1))
                    {
                        var str = command[pos];

                        if (str != null)
                        {
                            int i;

                            if (str != "ExtensionInfo")
                            {
                                if (str == "Plug")
                                {
                                    strArray = new string[Runtime.Repository.PlugInfos.Count];

                                    for (i = 0; i < strArray.Length; i++)
                                        strArray[i] = Runtime.Repository.PlugInfos[i].Name;

                                    return strArray;
                                }

                                if (str == "SlotInfo")
                                {
                                    strArray = new string[Runtime.Repository.SlotInfos.Count];

                                    for (i = 0; i < strArray.Length; i++)
                                        strArray[i] = Runtime.Repository.SlotInfos[i].Name;

                                    return strArray;
                                }
                            }
                            else
                            {
                                strArray = new string[Runtime.Repository.ExtensionInfos.Count];

                                for (i = 0; i < strArray.Length; i++)
                                    strArray[i] = Runtime.Repository.ExtensionInfos[i].Name;
                            }
                        }

                        return strArray;
                    }

                    return new string[0];
            }

            return null;
        }

        private string Unplug(string[] command)
        {
            string str;

            if (command.Length < 2)
                return "Too few arguments.";

            if (!RuntimeCommands.ContainsArgument(command[1], _arguments[0]))
                return string.Format("Invalid argument \"{0}\".", command[1]);

            if (command.Length < 3)
                return "Too few arguments.";

            var elements = RuntimeCommands.Find(command, 1, out str);

            if (elements.Length == 0)
                return str;

            var str3 = command[1];
            
            if (str3 != null)
            {
                RepositoryElement[] elementArray2;

                if (str3 != "ExtensionInfo")
                {
                    switch (str3)
                    {
                        case "Plug":
                            if (command.Length < 4)
                                foreach (PlugInfo pi in elements)
                                    pi.Unplug();
                            else
                            {
                                if (!command[3].Equals("from", StringComparison.CurrentCultureIgnoreCase))
                                    return "\"from\" expected.{0}";

                                if (command.Length < 5)
                                    return "Too few arguments.";

                                if (command[4] != "SlotInfo")
                                    return "\"SlotInfo\" argument expected.{0}";

                                if (command.Length < 6)
                                    return "Too few arguments.";

                                elementArray2 = RuntimeCommands.Find(command, 4, out str);

                                foreach (PlugInfo plug in elements)
                                    foreach (SlotInfo slot in elementArray2)
                                        plug.Unplug(slot);
                            }
                            break;
                        
                        case "SlotInfo":
                            if (command.Length < 4)
                                foreach (SlotInfo si in elements)
                                    si.Unplug();
                            else
                            {
                                if (!command[3].Equals("from", StringComparison.CurrentCultureIgnoreCase))
                                    return "\"from\" expected.{0}";

                                if (command.Length < 5)
                                    return "Too few arguments.";

                                if (command[4] != "Plug")
                                    return "\"Plug\" argument expected.{0}";

                                if (command.Length < 6)
                                    return "Too few arguments.";

                                elementArray2 = RuntimeCommands.Find(command, 4, out str);

                                foreach (SlotInfo slot in elements)
                                    foreach (PlugInfo plug in elementArray2)
                                        slot.Unplug(plug);
                            }
                            break;
                    }
                }
                else if (command.Length < 4)
                    foreach (ExtensionInfo ei in elements)
                        ei.UnplugPlugs();
                else
                {
                    if (!command[3].Equals("from", StringComparison.CurrentCultureIgnoreCase))
                        return "\"from\" expected.{0}";

                    if (command.Length < 5)
                        return "Too few arguments.";

                    if (command[4] != "ExtensionInfo")
                        return "\"ExtensionInfo\" argument expected.{0}";

                    if (command.Length < 6)
                        return "Too few arguments.";

                    elementArray2 = RuntimeCommands.Find(command, 4, out str);

                    foreach (ExtensionInfo extension1 in elements)
                        foreach (ExtensionInfo extension2 in elementArray2)
                            extension1.UnplugPlugs(extension2);
                }
            }

            return string.Empty;
        }

        public CommandHandler Handler
        {
            get { return Unplug; }
        }

        public string Help
        {
            get { return "  Help for command \"unplug\""; }
        }

        public string Name
        {
            get { return "unplug"; }
        }
    }
}

